﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace MikroMagazaObject
{
    public class Security
    {
        public string Server { get; set; }
        public string Db { get; set; }

        public Security(string server, string db)
        {
            try
            {
                Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID");
                this.Server = server;
                this.Db = db;

                Thread trd = new Thread(CheckEvaluate);
                trd.Start();
            }
            catch (Exception)
            {
            }
        }

        public void CheckEvaluate()
        {
            if (!Check())
            {
                Environment.Exit(Environment.ExitCode);
            }
        }

        bool Check()
        {
            try
            {

                WebClient webClient = new WebClient();
                Stream stream = webClient.OpenRead(string.Format("http://lisans.mikroyardim.com/Query.aspx?pc={0}&db={1}&srv={2}&tip=0", Environment.MachineName, this.Db, this.Server));
                System.IO.StreamReader sr = new System.IO.StreamReader(stream);
                String request = sr.ReadToEnd().Trim();
                switch (request)
                {
                    case "0": //onay bekliyor
                        if (DateTime.Now >= new DateTime(2014, 6, 1))
                        {
                            Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "{C512273G-38F1-45b2-836E-346F894B0143}");
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    case "1": //lisanslı program
                        Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "{C512273G-0000-45b2-826E-346F894B0143}");
                        return true;
                    case "2": //lisans bitti
                        Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "{C512273G-38F1-45b2-836E-346F894B0143}");
                        return false;
                    default:
                        if (request.IndexOf("lisans.mikroyardim") > -1)
                        {
                            if (DateTime.Now >= new DateTime(2014, 6, 1))
                            {
                                Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "{C512273G-38F1-45b2-836E-346F894B0143}");
                                return false;
                            }
                            else
                            {
                                return true;
                            }
                        }
                        else
                        {
                            System.Windows.Forms.MessageBox.Show(request);
                            return false;
                        }
                }
            }
            catch (Exception)
            {
                if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValueNames().Length > 0)
                {
                    if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValue("").ToString() == "{C512273G-0000-45b2-826E-346F894B0143}")
                    {
                        return true;
                    }
                    else if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValue("").ToString() == "{C512273G-38F1-45b2-836E-346F894B0143}")
                    {
                        if (DateTime.Now >= new DateTime(2014, 6, 1))
                        {
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return true;
                }
            }
        }
    }
}
